﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class Button : Sprite2D
    {
    #region Properties
        private String _text;
        public String Text
        {
            get { return _text; }
            set { _text = value; }
        }

        private SpriteFont _font;
        public SpriteFont Font
        {
            get { return _font; }
            set { _font = value; }
        }

        private float _zoom;
        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; }
        }

        private Color _colorTexture;
        public Color ColorTexture
        {
            get { return _colorTexture; }
            set { _colorTexture = value; }
        }

        private Color _colorText;
        public Color ColorText
        {
            get { return _colorText; }
            set { _colorText = value; }
        }

        private bool _haveAnimation = true;
        public bool HaveAnimation
        {
            get { return _haveAnimation; }
            set { _haveAnimation = value; }
        }

        private bool _haveShadow = true;
        public bool HaveShadow
        {
            get { return _haveShadow; }
            set { _haveShadow = value; }
        }

        private bool _haveSound = true;
        public bool HaveSound
        {
            get { return _haveSound; }
            set { _haveSound = value; }
        }
    #endregion

    #region Delegate & Event - Button is selected
        public delegate void IsSelected (int input);
        public event IsSelected Selected;
        /*
         * button.Selected += new Button.IsSelected(FunctionIsCalled);         
         */
    #endregion

    #region  Initiation
        public Button(Texture2D texture,Vector2 posotion)      
            : base(texture, posotion)            
        {
            _position = posotion;
            _texture = texture;
            _text = "";
            _rectangle = new Rectangle
                ((int)_position.X, (int)_position.Y, _texture.Width, _texture.Height);
            _scaleRect = 0;
            _zoom = 1;
            _colorTexture = Color.White;            
        }

        public Button(string text, SpriteFont font, Vector2 position)
            : base(null,position)
        {
            _position = position;
            _text = text;
            _font = font;
            _rectangle = new Rectangle
                ((int)_position.X, (int)_position.Y, _text.Length * 35, 50);
            _scaleRect = 0;
            _colorText = Color.Black;
        }

        public Button(string text, SpriteFont font, Vector2 position, Color colorText)
            : base(null, position)
        {
            _position = position;
            _text = text;
            _font = font;
            _rectangle = new Rectangle
                ((int)_position.X, (int)_position.Y, _text.Length * 35, 50);
            _scaleRect = 0;
            _colorText = colorText;
        }
    #endregion

    #region Methods
        public bool ButtonIsTouched(Vector2 touchPosition,int input)
        {
            bool result = false;
            if (PointInRectangle(touchPosition, _rectangle, -3))
            {
                result = true;
                _zoom = 1.3f;
                if (Selected != null)
                {
                    if (ManagerGame.SoundOn && _haveSound)
                        ManagerGame.managerSound._select.Play();
                    Selected(input);
                }
            }
            return result;
        }
    #endregion

    #region Update & Draw
        public override void Draw(SpriteBatch spritebatch, GameTime gametime)
        {
            _zoom -= 0.01f;
            if (_zoom <= 1) _zoom = 1;
            double time = gametime.TotalGameTime.TotalSeconds;
            float pulsate = (float)Math.Sin(time * 4) + 1;
            float scale = (1 + pulsate * 0.02f) * _zoom;
            if (!_haveAnimation) scale = 1;
            if (_text != "")
            {
                if (_haveAnimation && _haveShadow)
                    spritebatch.DrawString(_font, _text, _position + new Vector2(3, 3), Color.White, 0, Vector2.Zero, scale,SpriteEffects.None, 0);
                spritebatch.DrawString(_font, _text, _position, _colorText, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                return;
            }
            else
                spritebatch.Draw(_texture, _position, null, _colorTexture, 0, Vector2.Zero, scale,SpriteEffects.None, 0);
        }
    #endregion
    }
}
